# -*- coding: utf-8 -*-
"""
自定义管道组件
"""

import os
import ppc
import ppc.pipe


class DiskFree(ppc.pipe.Pipe):
    """磁盘空间信息"""

    def handle(self):
        path = self['path'].value
        df = os.statvfs(path)
        unit = self['unit'].value
        size = float(ppc.BYTES_NUM[unit])

        data = {}
        data['capacity'] = df.f_frsize * df.f_blocks / size
        data['available'] = df.f_frsize * df.f_bavail / size
        data['used'] = data['capacity'] - data['available']

        if data['capacity'] > 0:
            data['percent'] = (data['used'] / data['capacity']) * 100
        else:
            data['percent'] = 0

        for k, v in data.items():
            data[k] = int(round(v))

        self.transfer(data)
